﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using VietStar.Library.Entity;
using VietStar.Library.Commom.SqlServer;

namespace VietStar.Library.Data
{
    public class KhuyenMaiData
    {
        public KhuyenMaiData() { }
        Connection cn = new Connection();
        KhuyenMaiEntity entity = new KhuyenMaiEntity();

        public bool Insert(KhuyenMaiEntity entity)
        {
            try
            {
                using (SqlConnection connection = cn.ReadFromWebConfig())
                {
                    SqlCommand cmd = new SqlCommand("KhuyenMai_Insert", connection);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@SanPham", entity.SanPham));
                    cmd.Parameters.Add(new SqlParameter("@KhuyenMai", entity.KhuyenMai));
                    cmd.ExecuteNonQuery();
                    //done
                    connection.Close();
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public bool Update(KhuyenMaiEntity entity)
        {
            try
            {
                using (SqlConnection connection = cn.ReadFromWebConfig())
                {
                    SqlCommand cmd = new SqlCommand("KhuyenMai_Update", connection);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@SanPham", entity.SanPham));
                    cmd.Parameters.Add(new SqlParameter("@KhuyenMai", entity.KhuyenMai));
                    cmd.ExecuteNonQuery();
                    //done
                    connection.Close();
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public bool Delete(int Id)
        {
            try
            {
                using (SqlConnection connection = cn.ReadFromWebConfig())
                {
                    SqlCommand cmd = new SqlCommand("KhuyenMai_Delete", connection);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@Id", Id));
                    cmd.ExecuteNonQuery();
                    //done
                    connection.Close();
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public List<KhuyenMaiEntity> GetAll()
        {
            List<KhuyenMaiEntity> list = new List<KhuyenMaiEntity>();
            try
            {
                using (SqlConnection connection = cn.ReadFromWebConfig())
                {
                    SqlCommand cmd = new SqlCommand("KhuyenMai_All", connection);
                    cmd.CommandType = CommandType.StoredProcedure;
                    if (connection.State == ConnectionState.Closed)
                        connection.Open();
                    using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        while (reader.Read())
                        {
                            list.Add(EntityFromReader(reader));
                        }
                    }
                }
            }
            catch (Exception ex)
            { }
            return list;
        }
        public List<KhuyenMaiEntity> GetById(int Id)
        {
            List<KhuyenMaiEntity> list = new List<KhuyenMaiEntity>();
            try
            {
                using (SqlConnection connection = cn.ReadFromWebConfig())
                {
                    SqlCommand cmd = new SqlCommand("KhuyenMai_ById", connection);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@IDSanPham", Id));
                    if (connection.State == ConnectionState.Closed)
                        connection.Open();
                    using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        while (reader.Read())
                        {
                            list.Add(EntityFromReader(reader));
                        }
                    }
                }
            }
            catch (Exception ex)
            { }
            return list;
        }
        private KhuyenMaiEntity EntityFromReader(IDataReader reader)
        {
            KhuyenMaiEntity entity = new KhuyenMaiEntity();
            if (!reader.IsClosed)
            {
                if (reader["Id"] != null)
                    entity.Id = (int)reader["Id"];
                if (reader["SanPham"] != null)
                    entity.SanPham = (int)reader["SanPham"];
                if (reader["KhuyenMai"] != null)
                    entity.KhuyenMai = (string)reader["KhuyenMai"];
            }
            return entity;
        }
    }
}
